package com.hnkywl.sts.action.wlgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.xsgl.Psd;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.wlgl.XspsService;
import com.hnkywl.sts.service.xsgl.XsdService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午10:53
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/wlgl/xsps")
public class XspsAction extends SimpleActionSupport {
    private XspsService xspsService;
    private XsdService xsdService;
    //操作类型 add表示新增,update表示修改
    private String operation;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //行索引
    private int index;
    //传入的企业机构Id值
    private Integer type;
    //结算单号
    private String psdh;
    //客户的ID
    private Long glqyId;
    //发票类型ID
    private Long fpzlId;
    //客户的名称
    private String glqyQc;
    //公司的名称
    private String gsJc;
    //申请（查询）开始时间
    private Date sqrqStart;
    //申请（查询）结束时间
    private Date sqrqEnd;
    //销售配送单实体
    private Psd xsps;
    //销售配送单的ID
    private Long xspsId;
    //申报人的Id
    private Long sprId;
    //申报人所在公司的Id
    private Long gsId;
    //状态
    private Integer zt;
    //销售单
    private Xsd xsd;
    //销售单Id
    private Long xsdId;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private Date currentTime = new Date();
    private XsdDao xsdDao;
    private String xsdh;

    //销售配送单的查询分页显示
    @Action("xspsList")
    public void xspsList() {
        this.renderJson(this.xspsService.findAllxsps(xsdh, psdh, page, rows));
    }

    @Action("xsps")
    public String xsps() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    @Action("xsps_print")
    public String xspsPrint() {
        xsps = this.xspsService.findXspsDetail(xspsId);
        return SUCCESS;
    }

    @Action("xsps_print_detail")
    public void xspsPrintDetail() {
        Gson gson = new Gson();
        List<Map> xsdmxList = this.xsdDao.findXsmxList(xspsService.findXspsDetail(xspsId).getXsd().getId());
        Double xscb = 0d;
        Integer js = 0;
        Double sbsl = 0d;
        Double ck_sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Double je = 0d;
        Double se = 0d;
        Double xse = 0d;
        Double kcjezh = 0d;
        String result = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
        // String json = gson.toJson(xsdmxList);
        StringBuffer sbTable = new StringBuffer();
        if (null != xsdmxList) {
            sbTable.append("<table id = \"xsdmxdetail\" class=\"tb intb\"><thead>");
            sbTable.append("<tr>");
            sbTable.append("<td width='60px'>物资编码</td><td width='70px'>物资名称</td><td  width='90px'>型号规格</td><td width='30px'>单位</td><td width='30px'>材质</td>" +
                    "  <td name = 'yfsl'>应发数量</td><td name ='sfsl'>实发数量</td><td name='kcdj'>库存单价</td><td name='kcje'>库存总金额</td>" +
                    "  <td width='70px' name='csdj'>出售单价</td><td width='70px' name='csje'>出售总金额</td>");
            sbTable.append("</tr></thead><tbody>");

            for (int i = 0; i < xsdmxList.size(); i++) {
                sbTable.append("<tr>");
                // 物资编码
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.oldcode")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.oldcode"));
                }
                sbTable.append("</td>");
                //    品名
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.name")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");
                //  规格型号
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");
                //单位
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.dw")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.dw"));
                }
                sbTable.append("</td>");
                //材质
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.cz")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.cz"));
                }
                sbTable.append("</td>");
                // 应发数量
                sbTable.append("<td name='yfsl'>");
                if (null != xsdmxList.get(i).get("sbsl")) {
                    Double msbsl = (Double) xsdmxList.get(i).get("sbsl");
                    BigDecimal bsbsl = new BigDecimal(msbsl);
                    Double dsbsl = bsbsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dsbsl));
                    sbsl += dsbsl;
                }
                sbTable.append("</td>");
                //实发数量
                sbTable.append("<td name = 'sfsl'> ");
                if (null != xsdmxList.get(i).get("ck_sbsl")) {
                    if (((Double) xsdmxList.get(i).get("ck_sbsl")).equals(0d)) {
                        sbTable.append("");
                    } else {
                        Double mcksbsl = (Double) xsdmxList.get(i).get("ck_sbsl");
                        BigDecimal bcksbsl = new BigDecimal(mcksbsl);
                        Double dcksbsl = bcksbsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                        sbTable.append(df.format(dcksbsl));
                        ck_sbsl += dcksbsl;
                    }
                }
                sbTable.append("</td>");
                //库存单价
                sbTable.append("<td name='kcdj'>");
                if (null != xsdmxList.get(i).get("xscb")) {
                    Double mxscb = (Double) xsdmxList.get(i).get("xscb");
                    BigDecimal bxscb = new BigDecimal(mxscb);
                    Double dxscb = bxscb.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(df.format(dxscb));
                    xscb += dxscb;
                }
                sbTable.append("</td>");
                //库存金额
                sbTable.append("<td name='kcje'>");
                if (null != xsdmxList.get(i).get("xscb") && null != xsdmxList.get(i).get("ck_sbsl")) {
                    Double mxscb = (Double) xsdmxList.get(i).get("xscb");
                    BigDecimal bxscb = new BigDecimal(mxscb);
                    Double dxscb = bxscb.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    Double mcksbsl = (Double) xsdmxList.get(i).get("ck_sbsl");
                    BigDecimal bcksbsl = new BigDecimal(mcksbsl);
                    Double dcksbsl = bcksbsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();

                    Double mkcje = (dxscb * dcksbsl);
                    BigDecimal bkcje = new BigDecimal(mkcje);
                    Double dkcje = bkcje.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(jtdf.format(dkcje));
                    kcjezh += dkcje;
                }
                sbTable.append("</td>");

                sbTable.append("</td>");
                //出售单价（含税）
                sbTable.append("<td name='csdj'> ");
                if (null != xsdmxList.get(i).get("hsj")) {
                    sbTable.append(df.format(xsdmxList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                sbTable.append("</td>");
                // 出售金额（含税额）
                sbTable.append("<td name='csje'> ");
                if (null != xsdmxList.get(i).get("hsj") && null != xsdmxList.get(i).get("sbsl")) {
                    Double mxscb = (Double) xsdmxList.get(i).get("hsj");
                    BigDecimal bxscb = new BigDecimal(mxscb);
                    Double dxscb = bxscb.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    Double mcksbsl = (Double) xsdmxList.get(i).get("sbsl");
                    BigDecimal bcksbsl = new BigDecimal(mcksbsl);
                    Double dcksbsl = bcksbsl.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();

                    Double hse = (dxscb * dcksbsl);
                    BigDecimal bhsez = new BigDecimal(hse);
                    Double dbhsez = bhsez.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    sbTable.append(jtdf.format(dbhsez));
                    se += dbhsez;
                }
                sbTable.append("</td>");
                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"5\">合计:");
            sbTable.append("</th>");

            sbTable.append("<td name ='yfsl'>");
            sbTable.append(df.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td name = 'sfsl'>");
            if (ck_sbsl == null || ck_sbsl.equals(0d))
                sbTable.append("");
            else
                sbTable.append(df.format(ck_sbsl));
            sbTable.append("</td>");

            sbTable.append("<td name='kcdj'>");
            sbTable.append("</td>");

            sbTable.append("<td name='kcje'>");
            sbTable.append(jtdf.format(kcjezh));
            sbTable.append("</td>");

            sbTable.append("<td name='csdj'>");
            sbTable.append("</td>");

            sbTable.append("<td name='csje'>");
            sbTable.append("</td>");

            sbTable.append("</tr>");
            sbTable.append("</tbody></table>");
        }
        renderHtml(sbTable.toString());
    }

    @Action("xsps_content")
    public String xspsContent() {
        xsd = xsdService.findXsdDetail(xsdId);
        //进入update页面
        if (xspsId != null) {
            xsps = xspsService.findXspsDetail(xspsId);
        } else {
            xsps = new Psd();
        }
        return SUCCESS;
    }

    @Action("xsps_content_print")
    public String xspsContentPrint() {
        if (xsdId != null) {
            xsd = xsdService.findXsdDetail(xsdId);
        } else {
            xsd = new Xsd();
        }
        //进入update页面
        if (xspsId != null && xspsId != -1) {
            xsps = xspsService.findXspsDetail(xspsId);
        } else {
            xsps = new Psd();
        }
        return SUCCESS;
    }

    /**
     * 方法名:     addXsjsd
     * 功能描述:      增加销售单配送
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("add_xsps")
    public void addXsps() {
        try {
            this.xspsService.saveXsps(xsps);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加销售配送单成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     updateXsps
     * 功能描述:      修改销售单配送
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("update_xsps")
    public void updateXsps() {
        try {
            this.xspsService.updateXsps(xsps);
            renderText("{\"statusCode\":\"200\",\"message\":\"修改销售配送单成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     deleteXsps
     * 功能描述:      删除数据库销售单配送
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     */
    @Action("delete_xsps")
    public void deleteXsps() {
        try {
            this.xspsService.deleteXsps(xspsId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除销售配送单成功！\", \"operationType\":\"delete\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //==================get and set ================================


    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public Psd getXsps() {
        return xsps;
    }

    public void setXsps(Psd xsps) {
        this.xsps = xsps;
    }

    public String getXsdh() {
        return xsdh;
    }

    public void setXsdh(String xsdh) {
        this.xsdh = xsdh;
    }

    public Date getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(Date currentTime) {
        this.currentTime = currentTime;
    }

    public Long getXspsId() {
        return xspsId;
    }

    public void setXspsId(Long xspsId) {
        this.xspsId = xspsId;
    }

    public Long getSprId() {
        return sprId;
    }

    public void setSprId(Long sprId) {
        this.sprId = sprId;
    }

    public Integer getZt() {
        return zt;
    }

    public void setZt(Integer zt) {
        this.zt = zt;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public String getGlqyQc() {
        return glqyQc;
    }

    public void setGlqyQc(String glqyQc) {
        this.glqyQc = glqyQc;
    }

    public String getGsJc() {
        return gsJc;
    }

    public void setGsJc(String gsJc) {
        this.gsJc = gsJc;
    }

    public Date getSqrqStart() {
        return sqrqStart;
    }

    public void setSqrqStart(Date sqrqStart) {
        this.sqrqStart = sqrqStart;
    }

    public Date getSqrqEnd() {
        return sqrqEnd;
    }

    public void setSqrqEnd(Date sqrqEnd) {
        this.sqrqEnd = sqrqEnd;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getIndex() {
        return index;
    }

    public Long getGlqyId() {
        return glqyId;
    }

    public void setGlqyId(Long glqyId) {
        this.glqyId = glqyId;
    }

    public Xsd getXsd() {
        return xsd;
    }

    public void setXsd(Xsd xsd) {
        this.xsd = xsd;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getFpzlId() {
        return fpzlId;
    }

    public void setFpzlId(Long fpzlId) {
        this.fpzlId = fpzlId;
    }

    public String getPsdh() {
        return psdh;
    }

    public void setPsdh(String psdh) {
        this.psdh = psdh;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Long getXsdId() {
        return xsdId;
    }

    public void setXsdId(Long xsdId) {
        this.xsdId = xsdId;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setXspsService(XspsService xspsService) {
        this.xspsService = xspsService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }

    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }
}
